<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Lock;
use App\Models\Report;
use App\Models\Post;
use App\Models\Comment;
use App\Models\PointLog;
use App\User;
use Carbon\Carbon;
use App\Http\Resources\TeamUserResource;
use App\Http\Resources\DownPointResource;
use App\Http\Resources\PostResource;


class UserController extends Controller
{
  //当前登录用户
  public function getme(Request $request){
    $user = $request->user();
    $user->point_cash = $user->point / 100;
    if( $user->total_did != date('Ymd') ){
        $user->today_point = 0;
    }
    return $user;
  }
  
  // 所有文章
  public function posts(Request $request){
    $user = $request->user();
    
    $limit = intval($request->get('limit'));
    $limit =  $limit>5&&$limit<51?$limit:15;

    $data = Post::where('user_id',$user->id)->orderBy('id', 'desc')->simplePaginate($limit); //

    return response()->json(PostResource::collection($data));
  }
  
    
  public function asyncuserdata(Request $request){
    $app = \EasyWeChat\Factory::MiniProgram(config('wechat.mini_program.default'));
    $user = $request->user();
    $decryptedData = $app->encryptor->decryptData($user->session_key, $request->post('iv'), $request->post('ed'));
    $user->name = $decryptedData['nickName'];   
    $user->gender = $decryptedData['gender'];
    $user->city = $decryptedData['city'];
    $user->avatar = $decryptedData['avatarUrl'];
    $user->save();
    return response()->json(['ok']);
  }

}
